package DataStructure.hash;

import java.util.Scanner;

public class HashTableDemo {
    public static void main(String[] args) {
        HashTable hashTable = new HashTable(7);
        String key = "";
        Scanner scan = new Scanner(System.in);
        while (true) {
            System.out.println("add：添加");
            System.out.println("list：显示所有");
            System.out.println("exit：退出");
            key = scan.next();
            int id = 0;
            switch (key) {
                case "add":
                    System.out.println("请输入id");
                    id = scan.nextInt();
                    System.out.println("请输入名字");
                    String name = scan.next();
                    Emp emp = new Emp(id, name);
                    hashTable.add(emp);
                    break;
                case "list":
                    hashTable.list();
                    break;
                case "find":
                    System.out.println("请输入id");
                    id = scan.nextInt();
                    hashTable.findById(id);
                    break;
                case "exit":
                    scan.close();//关闭流
                    System.exit(0);//0为退出，1为进入
                default:
                    break;
            }
        }
    }

}

class Emp {
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

class HashTable {
    private EmpLinkedList[] empLinkedListArray;
    private int size;//相当于8

    public HashTable(int size) {
        this.size = size;
        empLinkedListArray = new EmpLinkedList[size];//只是初始化了一个数组对象
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();//将数组中的内容初始化
        }
    }

    public void findById(int id) {
        int no = hashFun(id);
        Emp emp = empLinkedListArray[no].findById(id);
        if (emp != null)
            System.out.printf("雇员id为%d,名字为%s\n", emp.id, emp.name);
    }

    /**
     * 添加
     *
     * @param emp
     */
    public void add(Emp emp) {
        int no = hashFun(emp.id);
        empLinkedListArray[no].add(emp);
    }

    public void list() {
        for (int i = 0; i < empLinkedListArray.length; i++) {
//            if (empLinkedListArray[i] != null) {//这句话没用，初始化之后一定是不为null
            empLinkedListArray[i].list(i);
//            }
        }
    }

    /**
     * 确定放到哪里去
     *
     * @param id
     * @return
     */
    public int hashFun(int id) {
        return id % size;
    }

}

class EmpLinkedList {

    private Emp head = null;

    /**
     * 添加到尾部
     *
     * @param emp
     */
    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp cur = head;
        while (true) {
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
        cur.next = emp;
    }

    public Emp findById(int id) {
        if (head == null) {
            System.out.println("未找到该雇员");
            return null;
        }
        Emp cur = head;
        while (true) {
            if (cur.id == id) {
                break;
            }
            cur = cur.next;
            if (cur == null) {
                System.out.println("未找到该雇员");
                break;
            }

        }
        return cur;
    }

    public void list(int no) {
        if (head == null) {
            System.out.println("链表" + no + "为空");
            return;
        }
        Emp cur = head;
        while (true) {
            System.out.printf("id：%d,name：%s\n", cur.id, cur.name);
            if (cur.next == null) {
                break;
            }
            cur = cur.next;
        }
    }
}
